﻿Imports System.Linq
Imports System.Data
Imports System.Web
Imports Equipment.DataAccess

Partial Public Class Holiday

#Region "Properties"
    Private _db As EquipmentDataContext
    Public Property DBContext As EquipmentDataContext
        Get
            Return _db
        End Get
        Set(ByVal value As EquipmentDataContext)
            _db = value
        End Set
    End Property

#End Region

    Public Shared Function RetrieveAll(ByVal startDate As DateTime, ByVal endDate As DateTime) As IEnumerable(Of Holiday)
        Dim db As EquipmentDataContext = Database.Instance
        Return From h In Database.Instance.Holidays Where h.HolidayDate >= startDate AndAlso h.HolidayDate <= endDate Order By h.HolidayDate
    End Function

    Public Shared Function RetrieveAll() As IEnumerable(Of Holiday)
        Dim db As EquipmentDataContext = Database.Instance
        Return db.Holidays.OrderBy(Function(h) h.HolidayDate)
    End Function

    Public Shared Function GetByPrimaryKey(ByVal id As Integer) As Holiday
        Dim _dataBase As EquipmentDataContext = Database.Instance
        Dim result = From h In _dataBase.Holidays Where h.ID = id
        Dim holidayEntity As Holiday = result.FirstOrDefault()
        holidayEntity.DBContext = _dataBase
        Return holidayEntity
    End Function

    Public Shared Function IsExistForDate(ByVal holidayDate As DateTime) As Boolean
        Return Database.Instance.Holidays.Count(Function(h) h.HolidayDate = holidayDate) > 0
    End Function

    Public Shared Function Insert(ByVal holidayDate As DateTime, ByVal description As String) As Holiday

        Dim _dataBase As EquipmentDataContext = Database.Instance
        Dim newHoliday As New Holiday

        newHoliday.HolidayDate = holidayDate
        newHoliday.Description = description

        _dataBase.Holidays.InsertOnSubmit(newHoliday)
        Try
            _dataBase.SubmitChanges()

        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try

        Return newHoliday
    End Function


    Public Sub Save()
        If Me.DBContext IsNot Nothing Then
            Dim _dataBase As EquipmentDataContext = Me.DBContext

            Try
                _dataBase.SubmitChanges()
            Catch ex As Exception
                ' logger.Error(exception.Message + ":" + exception.StackTrace);
            End Try
        End If

    End Sub

    Public Shared Sub Delete(ByVal id As Integer)

        Dim _dataBase As EquipmentDataContext = Database.Instance
        Dim holidayQuery = From p In _dataBase.Holidays
        Dim deletingHoliday As Holiday = holidayQuery.SingleOrDefault(Function(h) h.ID = id)

        _dataBase.Holidays.DeleteOnSubmit(deletingHoliday)

        Try
            _dataBase.SubmitChanges()

        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try

    End Sub

    Public Sub Delete()
        If Me.DBContext IsNot Nothing Then
            Me.DBContext.Holidays.DeleteOnSubmit(Me)

            Try
                Me.DBContext.SubmitChanges()
            Catch ex As Exception
                ' logger.Error(exception.Message + ":" + exception.StackTrace);
            End Try
        End If
    End Sub
End Class
